import React from 'react'
import { Button, Input, Form, Notify } from 'react-vant'
import { login } from '../../api'
import { LoginParamsType } from '../../types'
import { encryptedData } from '../../utils'
import { useAppDispatch } from '../../app/hooks'
import { userLogin } from '../../app/modules/userSlice'
import { useNavigate } from 'react-router-dom'

const initialValues: LoginParamsType = {
  email: '906446244@qq.com',
  pwd: '123456'
}
export default () => {
  const dispatch = useAppDispatch()
  const navigate = useNavigate()
  const [form] = Form.useForm()

  const onFinish = (values: LoginParamsType) => {
    dispatch(userLogin(values)).then(resp => {
      const { status, message } = resp.payload
      if (status === '0000') {
        navigate('/')
      } else {
        Notify.show({ type: 'danger', message })
      }
    })
  }

  return (
    <Form
      initialValues={initialValues}
      form={form}
      onFinish={onFinish}
      footer={
        <div style={{ margin: '16px 16px 0' }}>
          <Button round nativeType='submit' type='primary' block>
            提交
          </Button>
        </div>
      }
    >
      <Form.Item
        tooltip={{
          message:
            'A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself.',
        }}
        intro='确保这是唯一的用户名'
        rules={[{ required: true, message: '请填写用户名' }]}
        name='email'
        label='用户名'
      >
        <Input placeholder='请输入用户名' />
      </Form.Item>
      <Form.Item
        rules={[{ required: true, message: '请填写密码' }]}
        name='pwd'
        label='密码'
      >
        <Input placeholder='请输入密码' />
      </Form.Item>
    </Form>
  )
}
